package com.iring.service.common.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iring.domain.config.prop.LocalConfigProperties;
import com.iring.domain.config.prop.VOSPhoneProperties;
import com.iring.dao.common.OpenedServicesDao;
import com.iring.dao.common.view.ServiceViewDao;
import com.iring.dao.vos.CommonVosDao;
import com.iring.domain.common.DeviceInfo;
import com.iring.domain.common.OpenedServices;
import com.iring.domain.common.view.ServiceView;
import com.iring.domain.vos.phone.ModifyPhone;
import com.iring.exception.ParamInvalidException;
import com.iring.exception.config.ErrorEnum;
import com.iring.service.common.DeviceService;
import com.iring.service.common.SelectService;
import com.iring.util.common.CommonUtil;
import com.iring.vo.common.device.MacVO;

/**
 * @author wanggan
 */
@Service
public class SelectServiceImpl implements SelectService {

    private static final Logger logger = LoggerFactory.getLogger(SelectServiceImpl.class);

    @Autowired
    OpenedServicesDao openedServicesDao;

    @Autowired
    DeviceService deviceService;

    @Autowired
    ServiceViewDao serviceViewDao;

    @Autowired
    LocalConfigProperties localUserProperties;

    @Autowired
    VOSPhoneProperties vosPhoneProperties;

    @Autowired
    CommonVosDao commonVosDao;

    @Autowired
    CommonUtil commonUtil;

    private void checkStringParam(String str, String name) {
        if (str == null || str.trim()
                .equals("")) {
            throw new ParamInvalidException(name + " is null");
        }
    }

    private void checkMacParam(String mac){
        checkStringParam(mac, "mac");
        Boolean ismac = commonUtil.checkMac(mac);
        if(!ismac){
            throw new ParamInvalidException("invalid mac format");
        }
    }
    @Override
    public List<ServiceView> listPhoneService(MacVO macVO, String username) {
        String mac = macVO.getMac();
        checkMacParam(mac);
        return serviceViewDao.findByMacAndUsername(mac, username);
    }

    @Override
    public Boolean deleteGroupNetwork(MacVO macVO, String username) {
        String mac = macVO.getMac();
        checkMacParam(mac);

        OpenedServices orderServices = openedServicesDao.findByMacAndUsernameAndSid(mac, username,
                localUserProperties.getNetGroupOpt());
        if (null == orderServices) {
            throw new ParamInvalidException(ErrorEnum.INVALID_MAC);
        }
        boolean ischange = updateVosPhoneCallLevel(mac, vosPhoneProperties.getCallLevel());
        if (ischange) {
            openedServicesDao.delete(orderServices);
            logger.info("user ={} close mac={} group network", username, mac);
            return Boolean.TRUE;
        }
        throw new ParamInvalidException(ErrorEnum.CLOSE_GROUP_NETWORK_FAILED);

    }

    @Override
    public List<OpenedServices> listAllGroupNetwork(String username) {
        return openedServicesDao.findAllGroupNetwork(username);
    }

    @Transactional
    @Override
    public Boolean addGroupNetwork(MacVO macVO, String username) {
        String mac = macVO.getMac();
        checkMacParam(mac);
        Integer sid = localUserProperties.getNetGroupOpt();
        OpenedServices open = openedServicesDao.findByMacAndUsernameAndSid(mac,username,sid);
        if(null == open){
            boolean ischange = updateVosPhoneCallLevel(mac, localUserProperties.getNetGroupLevel());
            if (ischange) {
                // 开通服务成功
                OpenedServices openedServices = new OpenedServices();
                openedServices.setUsername(username);
                openedServices.setSid(sid);
                openedServices.setMac(mac);
                openedServices = openedServicesDao.save(openedServices);
                logger.info("open group-netword service success ={}", openedServices);
                // 扣除费用
                return Boolean.TRUE;
            }
        }
        throw new ParamInvalidException(ErrorEnum.OPEN_GROUP_NETWORK_FAILED);
    }

    /**
     * 修改话机的通话级别
     *
     * @param mac
     *            mac
     * @param level
     *            级别
     * @return 是否成功
     */
    private Boolean updateVosPhoneCallLevel(String mac, Integer level) {
        DeviceInfo deviceInfo = deviceService.findByMac(mac);
        if(null == deviceInfo){
            throw new ParamInvalidException(ErrorEnum.INVALID_MAC);
        }
        String e164s = deviceInfo.getIringtelNum();
        ModifyPhone modifyPhone = new ModifyPhone(e164s, level);
        return commonVosDao.modifyPhoneCallLevel(modifyPhone);

    }
}
